/** 
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
  *
 * Ray Yu & coffeefoam@126.com
 * Also see: http://www.opensource.com.cn/
 **/
package com.limit.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * 类型工具，提供类型转换，判断等接口
 * @author ray
 *
 */
public class TypeUtil
{
	/**
	 * 如果obj为空，则返回默认值
	 * @param obj
	 * @return
	 */
	public static int convertInt(Object obj, int defaultValue)
	{
		if (obj == null)
		{
			return defaultValue;
		}
		int convertValue = Integer.valueOf(String.valueOf(obj));
		
		return convertValue;
	}
	
	/**
	 * 如果obj为空，则返回默认值
	 * @param obj
	 * @param defaultValue
	 * @return
	 */
	public static double convertDouble(Object obj, double defaultValue)
	{
		if (obj == null)
		{
			return defaultValue;
		}
		double convertValue = Double.valueOf(String.valueOf(obj));
		
		return convertValue;
	}
	
	/**
	 * 如果obj为空，则返回默认值
	 * @param obj
	 * @param defaultValue
	 * @return
	 */
	public static long convertLong(Object obj, long defaultValue)
	{
		if (obj == null)
		{
			return defaultValue;
		}
		long convertValue = Long.valueOf(String.valueOf(obj));
		
		return convertValue;
	}
	
	/** 
	 * 字节数组转为字符串<br/>
	 * @description
	 * 
	 * @title  byteToString
	 */
	public static String byteToString(byte[] bt) {
		if(bt == null)
			return "";
		return new String(bt);
	}

    /**
     * 转换日期为compute_time
     * @param start 获取时间范围 0 每日0时0分，1当前时间 ，2每日23时59分
     * @param addDate 以当前日期为基准增减的时间
     * @return
     */
    public static long converDateToDBTime(int start,int addDate)
    {
        Calendar cal=java.util.Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE,addDate);
        Date nextDate=cal.getTime();
        int y =nextDate.getYear();
        int m = nextDate.getMonth();
        int n = nextDate.getDate();
        long ret =0;
        switch (start){
           case 0:
              ret = new Date(y,m,n,0,0).getTime()/1000;
               break;
           case 1:
               ret =  nextDate.getTime()/1000;
               break;
           case 2:
               ret = new Date(y,m,n,23,59,59).getTime()/1000;
               break;
        }
        return ret;
    }
}
